package com.spzx.product.controller;

import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.product.domain.Product;
import com.spzx.product.service.IProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 商品 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2025-04-28
 */
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController {
    @Autowired
    private IProductService productService;

    @Operation(summary = "查询商品列表")
    @GetMapping("/list")
    public TableDataInfo list() {
        startPage();
        List<Product> list = productService.selectProductList();
        return getDataTable(list);
    }

    @Operation(summary = "新增商品")
    @PostMapping
    public AjaxResult add(
            @Parameter(description = "商品")
            @RequestBody Product product) {
        return toAjax(productService.insertProduct(product));
    }

    @Operation(summary = "更新上下架状态")
    @GetMapping("/updateStatus/{id}/{status}")
    public AjaxResult updateStatus(
            @Parameter(description = "商品id") @PathVariable Long id,
            @Parameter(description = "状态") @PathVariable Integer status) {

        if(status != 1 && status != -1){
            //throw new ServiceException("非法参数");
            return AjaxResult.error("非法参数");
        }

        productService.updateStatus(id, status);
        return success();
    }

    @Operation(summary = "删除商品")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(
            @Parameter(description = "商品id列表")
            @PathVariable List<Long> ids) {
        return toAjax(productService.deleteProductByIds(ids));
    }

}
